package com.apexsoft.assets.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.apexsoft.assets.base.MybatisInterceptor;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 主数据源配置
 *
 * @author xiaohui
 * create on 2020-02-23
 */
@Configuration
@MapperScan(
        basePackages = {"com.apexsoft.assets"},
        annotationClass = Mapper.class,
        sqlSessionTemplateRef = "primarySqlSessionTemplate"
)
public class DatasourceConfig {

    public static final String DATABASE_MASTER = "masterDs";


    @Autowired
    private MybatisPlusProperties properties;
    @Autowired
    MybatisInterceptor mybatisInterceptor;

    @Bean(DATABASE_MASTER)
    @ConditionalOnProperty("spring.datasource.druid.url")
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource masterDs() {
        return new DruidDataSource();
    }


    @Primary
    @Bean("primarySqlSessionFactory")
    public MybatisSqlSessionFactoryBean primaryMybatisSqlSessionFactoryBean(@Qualifier(DATABASE_MASTER) DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
        mybatisPlus.setDataSource(dataSource);
        mybatisPlus.setVfs(SpringBootVFS.class);
        MybatisConfiguration mc = this.properties.getConfiguration();
        mc.setMapUnderscoreToCamelCase(true);
        mc.setReturnInstanceForEmptyRow(true);
        mc.addInterceptor(mybatisInterceptor);
//        mc.setLogImpl(StdOutImpl.class);
        mybatisPlus.setConfiguration(mc);
        mybatisPlus.setMapperLocations((new PathMatchingResourcePatternResolver())
                .getResources("classpath*:mapper/**/*.xml"));
        mybatisPlus.setPlugins(new Interceptor[] {new PaginationInterceptor()});
        return mybatisPlus;
    }

    @Bean(name = "primaryTransactionManager")
    public DataSourceTransactionManager primaryDataSourceTransactionManager(@Qualifier(DATABASE_MASTER) DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean("primarySqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Primary
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate loggerJdbcTemplate(@Qualifier(DATABASE_MASTER) DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
